package com.bestcem.xm.txtanls.grpc.convert;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.txtanls.entity.pojo.TopicDO;
import com.bestcem.xm.txtanls.grpc.v1.enums.TopicTypeEnum;
import com.bestcem.xm.txtanls.grpc.v1.services.ExportOrImportTopic;
import com.bestcem.xm.txtanls.service.dto.topic.TopicDTO;
import com.bestcem.xm.txtanls.service.dto.topic.TopicDataDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.bestcem.xm.common.core.constant.Constants.DEFAULT_ARRAY_SIZE;

/**
 * 话题dto转换类
 *
 * @author liheng
 * @version v1.0
 * @date 2021/6/3 20:39
 */

public class TopicConvert {
    private TopicConvert() {
    }


    /**
     * 话题表实体转dto
     *
     * @param entity 话题表实体类
     * @return 话题dto
     */
    public static TopicDTO convertDTO(TopicDO entity) {
        return new TopicDTO(entity.getId(), entity.getName(), entity.getTopicGID(), entity.getType(), entity.getKeyDict());
    }

    /**
     * 话题dto转话题grpc实体
     *
     * @param topicDTO 话题dto
     * @return 话题grpc实体
     */
    public static com.bestcem.xm.txtanls.grpc.v1.services.Topic toTopic(TopicDTO topicDTO) {
        com.bestcem.xm.txtanls.grpc.v1.services.Topic.Builder builder = com.bestcem.xm.txtanls.grpc.v1.services.Topic.newBuilder();
        if (com.bestcem.xm.txtanls.enums.TopicTypeEnum.valid(topicDTO.getType())) {
            builder.setType(TopicTypeEnum.TopicType.forNumber(topicDTO.getType()));
        }
        if (StringUtils.isNotBlank(topicDTO.getId())) {
            builder.setId(topicDTO.getId());
        }
        if (StringUtils.isNotBlank(topicDTO.getName())) {
            builder.setName(topicDTO.getName());
        }
        if (StringUtils.isNotBlank(topicDTO.getTopicGid())) {
            builder.setTopicGid(topicDTO.getTopicGid());
        }
        if (CollUtil.isNotEmpty(topicDTO.getKeyDict())) {
            builder.addAllKeyDict(topicDTO.getKeyDict());
        }
        return builder.build();
    }

    /**
     * 话题相关，话题DTO转为grpc传输类
     *
     * @param topicDTOS 话题DTO list
     * @return proto 话题message
     * @author liheng
     * @date 2021/5/13 21:13
     */
    public static List<com.bestcem.xm.txtanls.grpc.v1.services.Topic> convertTopicDTOList(List<TopicDTO> topicDTOS) {
        ArrayList<com.bestcem.xm.txtanls.grpc.v1.services.Topic> keywords = new ArrayList<>(topicDTOS.size());
        for (TopicDTO topicDTO : topicDTOS) {
            keywords.add(TopicConvert.toTopic(topicDTO));
        }
        return keywords;

    }

    /**
     * 话题导入grpc参数list转话题数据实体dto list
     *
     * @param topicDataDTOS 话题导入grpc参数list
     * @return 话题数据实体dto
     */
    public static List<TopicDataDTO> changeImportData(List<ExportOrImportTopic> topicDataDTOS) {
        List<TopicDataDTO> topicDataDTOList = new ArrayList<>(DEFAULT_ARRAY_SIZE);
        for (ExportOrImportTopic importTopic : topicDataDTOS) {
            TopicDataDTO topicData = new TopicDataDTO(importTopic.getGroupName(), importTopic.getName(), importTopic.getKeyword());
            topicDataDTOList.add(topicData);
        }
        return topicDataDTOList;
    }
}
